package d3_collection_test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * 2024/6/6 斗地主游戏的案例开发 Cjj 9:34
 */
public class Room {
    public static void main(String[] args) {

    }
    //必须有一副牌
    private List<Card> allCards=new ArrayList<>();

    public Room(){
        //1.做出54张牌, 存入到集合allCards
        //a.点数:个数确定了, 类型确定。
        String[] numbers={"3","4","5","6","7","8","9","10","J","Q","k","A","2"};

        //b.花色：个数确定了,类型确定。
        String[] colors={"♣","♥","♦","♠"};
         int size=0;
        //c.遍历点数,再遍历花色,组织牌
        for (String number : numbers) {
            //number=3
            size++;
            for (String color : colors) {
                Card c = new Card(number,color,size);
                allCards.add(c);//存入了牌
            }
        }

        //单独存入小大王的。
        Card c1 = new Card("", "🃏", ++size);
        Card c2 = new Card("", "🃏", ++size);
        Collections.addAll(allCards,c1,c2);
        System.out.println("新牌："+allCards);


    }

    /*游戏启动*/
    public void start() {
          //洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后："+allCards);

        //发牌 首先定义三个玩家
        List<Card> chengJiangJie = new ArrayList<>();
        List<Card> chengJiaHeng = new ArrayList<>();
        List<Card> chengZiTong = new ArrayList<>();

        //正式发牌给这三个玩家,依次发出51张牌,剩余3张做为底牌
        //allCards=[♦3, ♠2, ♣9, ♣k, ♠J, 🃏, ♣J, ♦Q, ♠10,...
        for (int i = 0; i < allCards.size()-3; i++) {
            Card card = allCards.get(i);
            if(i%3==0){
                chengJiangJie.add(card);
            }else if(i%3==1){
                chengJiaHeng.add(card);
            }else if(i%3==2){
              chengZiTong.add(card);

            }
        }

        //3.对三个玩家的牌进行排序
        sortCards(chengJiangJie);
        sortCards(chengJiaHeng);
        sortCards(chengZiTong);

        //4.看牌
        System.out.println("成姜捷：" + chengJiangJie);
        System.out.println("成佳恒：" + chengJiaHeng);
        System.out.println("成紫彤：" + chengZiTong);

        //把三张底牌截到集合里面去
        List<Card> cards = allCards.subList(allCards.size() - 3, allCards.size());//51,52,53
        System.out.println("底牌"+cards);
        chengJiaHeng.addAll(cards);
        System.out.println("成佳恒抢到地主后："+chengJiaHeng);
    }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize()-o1.getSize();//降序排序
            }
        });
    }
}
